from flask import Blueprint, request, jsonify

from services.location_service import LocationService

location_blueprint = Blueprint('location', __name__)

@location_blueprint.route('/get_nearest', methods=['POST'])
def get_nearest_attraction():
    try:
        user_location = request.json.get('location')
        if not user_location:
            return jsonify({'error': 'Missing location parameter'}), 400
            
        service = LocationService()
        nearest = service.get_nearest_attraction(user_location)
        
        if nearest:
            return jsonify({
                'name': nearest.attraction_name,
                'latitude': nearest.latitude,
                'longitude': nearest.longitude
            })
        return jsonify({'error': 'No attractions found'}), 404
        
    except Exception as e:
        return jsonify({'error': str(e)}), 500